package
{
	import nl.hyves.api.parameters.ResponseFields;
	import nl.hyves.api.core.HyvesAPI;
	import nl.hyves.api.core.IHyvesAPI;
	import nl.hyves.api.login.AIRLogin;
	import nl.hyves.api.login.ILogin;
	import nl.hyves.api.methods.Media;
	import nl.hyves.api.methods.Users;
	import nl.hyves.api.methods.options.SortType;
	import nl.hyves.api.net.IHyvesResponse;
	import nl.hyves.api.parameters.GlobalParameters;
	import nl.hyves.api.valueobjects.UserVO;

	import flash.display.Sprite;

	public class AIRLoginExample extends Sprite
	{
		private static const CONSUMER_KEY:String = 'NTY2MF99lTWIO2JXZC70FFqkUnHI';
		private static const CONSUMER_SECRET:String = 'NTY2MF8Tl4ZJC7AUWd5br7yCEVVs';
		private var service:IHyvesAPI;

		public function AIRLoginExample()
		{
			var methods:Vector.<String> = Vector.<String>([Users.GET_LOGGEDIN, Users.GET_FRIENDS_BY_LOGGEDIN_SORTED, Media.GET_UPLOAD_TOKEN, Media.GET]);

			service = new HyvesAPI(CONSUMER_KEY, CONSUMER_SECRET, false);

			if (service.isLoggedIn(methods))
			{
				onLoggedIn();
			}
			else
			{
				var login:ILogin = new AIRLogin(methods);
				service.login(login, onLoggedIn);
			}

			service.call(Users.getLoggedin(), onUser);
			service.call(Users.getFriendsByLoggedinSorted(SortType.ALPHABETICALLY), onFriends, new GlobalParameters().resultsPerPage(500).returnFields("/user/firstname", "/user/userid", "/user/profilepicture/square_extralarge/src").responseFields(ResponseFields.PROFILE_PICTURE));
		}

		private function onLoggedIn():void
		{
			trace('Authenticated! Logged in user has id: ' + service.accesstoken.userid);
		}

		private function onUser(response:IHyvesResponse):void
		{
			if (response.success)
			{
				var user:UserVO = response.data as UserVO;

				trace(user.firstname + ' ' + user.lastname + ' is logged in.');
			}
			else
			{
				trace(response.error);
			}
		}

		private function onFriends(response:IHyvesResponse):void
		{
			if (response.success)
			{
				trace(response.data);
			}
			else
			{
				trace(response.error);
			}
		}
	}
}